﻿using System.Windows.Controls;
using System.Windows.Navigation;
using AdminApp.Web.Services;

namespace AdminApp.Views
{
    public partial class AllEvents : Page
    {
        public AllEvents()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void allSlotsDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }

            CreateTree();
        }

        private void CreateTree()
        {
            DigitalSignageDomainContext context = 
                allSlotsDomainDataSource.DomainContext as DigitalSignageDomainContext;

            foreach (var slot in context.AllSlots)
            {
                TreeViewItem eventnode = GetEventNode(slot.EventName);

                TreeViewItem roomNode = GetRoomNode(eventnode, slot.RoomName);

                TreeViewItem dayNode = GetDayNode(roomNode, slot.SlotDate);

                TreeViewItem timeNode = GetTimeNode(dayNode, slot.SlotTime, slot.SessionTitle);

                //var sessionNode = GetSlotNode(roomNode, slot.SlotDate);
                //timeNode.Items.Add(slot.SessionTitle 
                //    + " - " + slot.SpeakerFirstname + " " + slot.SpeakerLastname);
            }
        }

        private TreeViewItem GetTimeNode(TreeViewItem dayNode, System.TimeSpan? time, string sessionTitle)
        {
            string title = time.ToString() + " - " + sessionTitle;
            foreach (var i in dayNode.Items)
            {
                TreeViewItem tb = i as TreeViewItem;
                if (tb.Header.ToString() == title)
                {
                    return i as TreeViewItem;
                }
            }
            TreeViewItem tvi = new TreeViewItem();
            tvi.Header = title;
            dayNode.Items.Add(tvi);

            return tvi;
        }

        private TreeViewItem GetDayNode(TreeViewItem roomNode, System.DateTime? date)
        {
            string title = date.Value.ToString("dd.MM.yyyy");
            foreach (var i in roomNode.Items)
            {
                TreeViewItem tb = i as TreeViewItem;
                
                if (tb.Header.ToString() == title)
                {
                    return i as TreeViewItem;
                }
            }
            TreeViewItem tvi = new TreeViewItem();
            tvi.Header = title;
            roomNode.Items.Add(tvi);

            return tvi;
        }

        private TreeViewItem GetRoomNode(TreeViewItem eventnode, string roomName)
        {
            foreach (var i in eventnode.Items)
            {
                TreeViewItem tb = i as TreeViewItem;
                if (tb.Header.ToString() == roomName)
                {
                    return i as TreeViewItem;
                }
            }
            TreeViewItem tvi = new TreeViewItem();
            tvi.Header = roomName;
            eventnode.Items.Add(tvi);

            return tvi;
        }

        private TreeViewItem GetEventNode(string eventName)
        {
            foreach (var i in _tree.Items)
            {
                TreeViewItem tb = i as TreeViewItem;
                if (tb.Header.ToString() == eventName)
                {
                    return i as TreeViewItem;
                }
            }
            TreeViewItem tvi = new TreeViewItem();
            tvi.Header = eventName;
            _tree.Items.Add(tvi);

            return tvi;
        }

    }
}
